package com.dfbz.dao;

import com.dfbz.dao.Base.BaseDao;
import com.dfbz.model.Article;
import com.dfbz.model.Favorite;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;

public class ArticleDao extends BaseDao<Article> {
    private JdbcTemplate jdbcTemplate=getJdbcTemplate();
    public ArticleDao() {
        super(Article.class);
    }
    public List<Article> findArticle(Integer id,String articleTitle, Integer startIndex, Integer pageSize){
        String sql="select * " +
                "from article " +
                "where title like ? limit ?,?";
        List<Article> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Article.class),  "%" + articleTitle + "%", startIndex, pageSize);
        return list;
    }
    public Integer countByArticle(Integer id,String article){
        String sql="select count(1) " +
                "from article " +
                "where user_id=? and title like ?";
        try {
            return jdbcTemplate.queryForObject(sql,Integer.class,id,"%"+article+"%");
        }catch (Exception e){
            return null;
        }
    }
    public Integer countByColArticle(Integer id,String title){
        String sql="select count(1) " +
                "from article a " +
                "where a.title like ? and a.id in (select f.a_id " +
                "from favorite f " +
                "where f.u_id=?) ";
        try {
            return jdbcTemplate.queryForObject(sql,Integer.class,"%"+title+"%",id);
        }catch (Exception e){
            return null;
        }
    }
    //收藏数
    public Integer countById(Integer id) {
        String sql="SELECT count(1) FROM `favorite` where a_id=?";
        try {
            return jdbcTemplate.queryForObject(sql,Integer.class,id);
        }catch (Exception e){
            return null;
        }
    }
    //是否收藏
    public Integer IsfavArticle(Integer uId,Integer aId){
        String sql="SELECT count(1) FROM `favorite` where u_id=? and a_id=?";
             try {
                 return  jdbcTemplate.queryForObject(sql,Integer.class,uId,aId);
             }catch (Exception e){
                 return null;
             }
    }
    //收藏该文章的朋友
    public ArrayList<Favorite> favArticleById(Integer aId, Integer uId){
        String sql="SELECT * FROM `favorite` f " +
                "where a_id=? and f.u_id in (select uf.user_focus_id " +
                "from userfocus uf " +
                "where user_id=?) ";
        ArrayList<Favorite> list = (ArrayList<Favorite>) jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(Favorite.class),aId,uId);
        return list;
    }
    //我收藏的文章
    public List<Article> findCollectArticle(Integer id,String articleTitle, Integer startIndex, Integer pageSize){
        String sql="select * " +
                "from article " +
                "where article.title like ? and exists (select favorite.a_id " +
                "from favorite " +
                "where favorite.u_id =? and article.id=favorite.a_id ) limit ?,? ";
        List<Article> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Article.class),"%"+articleTitle+"%",id,startIndex, pageSize);
        return list;
    }
    //
}
